package com.gx.smart.building.ui.spare

import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivitySelectSparePartBinding
import com.gx.smart.building.ui.spare.adapter.SelectSpareAdapter
import com.gx.smart.building.ui.spare.model.SelectedSpareStocksMessageEvent
import com.gx.smart.building.ui.spare.model.SpareStockMessageEvent
import com.gx.smart.building.ui.spare.viewmodel.SparePartViewModel
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareStockDto
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SelectSparePartActivity : BaseMVVMActivityV3<SparePartViewModel>(),
    View.OnClickListener,
    TextView.OnEditorActionListener,
    BaseAdapter.OnItemClickListener<SpareStockDto> {

    private lateinit var viewBinding: ActivitySelectSparePartBinding
    private lateinit var adapter: SelectSpareAdapter
    private lateinit var warehouseId: String
    var spareStocks: MutableMap<Int, SpareStockDto> = mutableMapOf()
    var positionList: MutableMap<Int, Int> = mutableMapOf()
    override fun onBindViewModel(): Class<SparePartViewModel> = SparePartViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_select_spare_part

    override fun getToolbarTitle(): String = "选择备件"

    override fun initContentView(root: ViewGroup?) {
        viewBinding = ActivitySelectSparePartBinding.inflate(layoutInflater, root, true)
        adapter = SelectSpareAdapter(this)
        viewBinding.recyclerView.adapter = adapter
        viewBinding.search.setOnEditorActionListener(this)
        viewBinding.clear.setOnClickListener(this)
        adapter.setItemClickListener(this)
    }

    override fun initData() {
        warehouseId = intent.getStringExtra("warehouseId")!!
        viewModel.querySpareStockList(warehouseId).observe(this) {
            if (it.isNullOrEmpty()) {
                showNoDataView(true)
            } else {
                adapter.addAll(it)
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.clear -> viewBinding.search.editableText.clear()
        }
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            search()
            KeyboardUtils.hideSoftInput(v!!)
            return true
        }
        return false
    }

    private fun search() {
        val keyword = viewBinding.search.text.toString().trim()
        viewModel.querySpareStockList(keyword, warehouseId).observe(this) {
            if (it.isNullOrEmpty()) {
                showNoDataView(true)
            } else {
                adapter.refresh(it)
            }
        }
    }

    override fun onItemClick(e: SpareStockDto, position: Int) {
        val selectedPosition = intent.getIntExtra("position", -1)
        if (positionList.isNotEmpty()
            && positionList.values.contains(position)
        ) {
            ToastUtils.showLong("请重新选择")
            return
        }
        spareStocks[selectedPosition] = e
        positionList[selectedPosition] = position
        EventBus.getDefault().postSticky(SpareStockMessageEvent(spareStocks, positionList))
        finish()
    }

    override fun initView() {
        registerEventBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun messageEvent(event: SelectedSpareStocksMessageEvent) {
        spareStocks.putAll(event.item)
        positionList.putAll(event.positionList)
    }
}